package com.ctrlcar.giftrecord.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctrlcar.giftrecord.pojo.Relative;
import com.ctrlcar.giftrecord.pojo.model.Result;
import com.ctrlcar.giftrecord.service.RelativeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: zengxz
 * @Description: 亲朋表控制层
 * @DateTime: 2025-03-27 9:40
 **/
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/receive")
public class RelativeController {
    @Resource
    private RelativeService relativeService;

    /**
     * 根据 亲朋id获取亲朋详细信息
     * @param relativeId
     * @return
     */
    @GetMapping
    public Result<Relative> getRelative(@RequestParam Integer relativeId){
        return Result.success(relativeService.getById(relativeId));
    }

    /**
//     * 获取用户的所有好友
//     * @param userId
//     * @return
//     */
//    @GetMapping("/list")
//    public Result<List<Relative>>  getAllRelative(@RequestParam Integer userId){
//        List<Relative> list = relativeService.list(new LambdaQueryWrapper<Relative>().eq(Relative::getUserId, userId));
//        if(list.size()>0){
//            return Result.success(list);
//        }
//        return  Result.error("未查询到相关数据");
//    }

    /**
     * 获取用户的所有亲朋，支持地址或名字模糊查询
     * @param userId
     * @param sel
     * @return
     */
    @GetMapping("/list")
    public Result<List<Relative>> getAllRelative(
            @RequestParam Integer userId,
            @RequestParam(required = false) String sel) {
//        名字
        LambdaQueryWrapper<Relative> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Relative::getUserId, userId); // 筛选指定用户ID的亲朋
        // 如果提供了名字参数，则添加名字模糊查询条件
        if (sel != null && !sel.isEmpty()) {
            queryWrapper.like(Relative::getName, sel);
        }
////地址
//        LambdaQueryWrapper<Relative> queryWrapper1 = new LambdaQueryWrapper<>();
//        queryWrapper1.eq(Relative::getUserId, userId); // 筛选指定用户ID的亲朋
//        // 如果提供了名字参数，则添加名字模糊查询条件
//        if (sel != null && !sel.isEmpty()) {
//            queryWrapper1.like(Relative::getName, sel);
//        }

        List<Relative> list = relativeService.list(queryWrapper);
//        List<Relative> list1 = relativeService.list(queryWrapper1);
//        list.addAll(list1);
        if (list.size() > 0) {
            return Result.success(list);
        }
        return Result.error("未查询到相关数据");
    }
    /**
     * 修改亲朋信息
     * @param relative
     * @return
     */
    @PutMapping
    public Result  updateRelative(@RequestBody Relative relative){
        relativeService.updateById(relative);
        return Result.success(relative);
    }

    /**
     * 根据亲朋id删除亲朋
     * @param relativeId
     * @return
     */
    @DeleteMapping
    public Result  deleteRelative(@RequestParam Integer relativeId){
       Result result =  relativeService.delectByrelativeId(relativeId);
        return result;
    }

    /**
     * 模糊查询亲朋
     * @param name
     * @param address
     * @return
     */
    @GetMapping("/search")
    public Result<List<Relative>>  searchRelative(@RequestParam String name ,@RequestParam String address){
        List<Relative> list = relativeService.list(
                new LambdaQueryWrapper<Relative>()
                        .like(Relative::getName, name)
                        .like(Relative::getAddress, address)
         );
        if(!list.isEmpty()){
            return Result.success(list);
        }
        return  Result.error("未查询到相关数据");
    }

}
